#include <bits/stdc++.h>

using namespace std;

int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

int main()
{
    int m, n;
    cin >> m >> n;
    vector<vector<int>> graph(m, vector<int>(n, 0));
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> graph[i][j];
        }
    }
    vector<vector<bool>> visited(m, vector<bool>(n, false));
    queue<pair<int, int>> que;
    int perimeter = 0;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (graph[i][j] == 1 && visited[i][j] == false)
            {
                que.push({i, j});
                visited[i][j] = true;
                while (!que.empty())
                {
                    pair<int, int> pair = que.front();
                    que.pop();
                    int count = 4;
                    int row = pair.first;
                    int col = pair.second;
                    for (int i = 0; i < 4; i++)
                    {
                        int next_row = row + dir[i][0];
                        int next_col = col + dir[i][1];
                        if (next_row >= 0 && next_row < m && next_col >= 0 && next_col < n)
                        {
                            if (graph[next_row][next_col] == 1)
                            {
                                count--;
                                if (visited[next_row][next_col] == false)
                                {
                                    visited[next_row][next_col] = true;
                                    que.push({next_row, next_col});
                                }
                            }
                        }
                    }
                    perimeter += count;
                }
            }
        }
    }
    cout << perimeter << endl;
    return 0;
}